package com.yunjian.admin.config.log;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.log4j.Log4JLoggingSystem;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@SuppressWarnings("deprecation")
public class ChangeLogMvcEndpoint extends EndpointMvcAdapter {
	public ChangeLogMvcEndpoint(ChangeLogEndpoint delegate) {
		 super(delegate);
	}

	@RequestMapping(value = "/{level}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Object changeLog(@RequestParam(name = "package", required = false)String packageName,@PathVariable String level) {
		Map<String, String> result = new HashMap<String, String>();
		boolean success = true;
		try {
			Log4JLoggingSystem log4JLoggingSystem = new Log4JLoggingSystem(this.getClass().getClassLoader());
			log4JLoggingSystem.setLogLevel(packageName == null || "".equals(packageName.trim()) ? "" : packageName, LogLevel.valueOf(level.toUpperCase()));
		} catch (Exception e) {
		    success = false;
		}
		result.put("success", String.valueOf(success));
		return result;
	}
}
